package com.kingdee.eas.custom.hr.attendance.app;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.common.DateUtils;
import com.kingdee.eas.custom.hr.attendance.CalendarTypeInfo;
import com.kingdee.eas.custom.hr.attendance.PersonScheduleFactory;
import com.kingdee.eas.custom.hr.attendance.PersonScheduleInfo;
import com.kingdee.eas.custom.hr.attendance.SchedulDetailCollection;
import com.kingdee.eas.custom.hr.attendance.SchedulDetailFactory;
import com.kingdee.eas.custom.hr.attendance.SchedulDetailInfo;
import com.kingdee.eas.custom.hr.attendance.WorkforceManagementInfo;
import com.kingdee.eas.custom.hr.attendance.WorkingTimeZoneFlucationEntryInfo;
import com.kingdee.eas.custom.hr.attendance.WorkingTimeZoneMgrInfo;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.hr.base.HRBillBaseEntryInfo;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.eas.hr.base.app.HRBillBizBean;
import com.kingdee.eas.hr.rec.util.DateUtil;
import com.kingdee.eas.util.app.DbUtil;

public class WorkingTimeZoneFlucationBizBean extends HRBillBizBean {

	private Map map = new HashMap();

	public void _addnew(Context arg0, IObjectPK arg1, IObjectValue arg2)
			throws BOSException, EASBizException {
		// TODO Auto-generated method stub

	}

	public void _beforeEffectBeforeBackJob(Context arg0,
			HRBillBaseEntryInfo arg1) throws BOSException, EASBizException {
		// TODO Auto-generated method stub

	}

	public void _beforeEffectOnBackJob(Context arg0, HRBillBaseEntryInfo arg1)
			throws BOSException, EASBizException {
		// TODO Auto-generated method stub

	}

	public void _beforeEffectOnNoJob(Context arg0, HRBillBaseEntryInfo arg1)
			throws BOSException, EASBizException {
		// TODO Auto-generated method stub

	}

	public void _check(Context arg0, IObjectPK arg1, IObjectValue arg2)
			throws BOSException, EASBizException {
		// TODO Auto-generated method stub

	}

	public void _delete(Context arg0, IObjectPK arg1) throws BOSException,
			EASBizException {
	}

	public void _entryEffect(Context ctx, HRBillBaseEntryInfo entryInfo)
			throws BOSException, EASBizException {
		WorkingTimeZoneFlucationEntryInfo info = (WorkingTimeZoneFlucationEntryInfo) entryInfo;
		PersonInfo person = info.getPerson();
		person.setWorkforceManagement(info.getNewWorkforceManagement());
		person.setWorkGroup(info.getNewGroup());

		SelectorItemCollection selectors = new SelectorItemCollection();
		selectors.add("adminOrgBase");
		selectors.add("workforceManagement");
		selectors.add("workGroup");

		PersonFactory.getLocalInstance(ctx).updatePartial(person, selectors);

		generateScheduleTable(ctx, person, info.getBeginDate());
	}

	private void generateScheduleTable(Context ctx, PersonInfo personInfo,
			Date beginDate) throws BOSException, EASBizException {

		System.out.println(personInfo.getId());
		WorkforceManagementInfo workforceManagementInfo = personInfo
				.getWorkforceManagement();
		String group = personInfo.getWorkGroup();

		String key = workforceManagementInfo.getId().toString() + "|" + group;

		SchedulDetailCollection collection = null;

		if (map.get(key) != null) {
			collection = (SchedulDetailCollection) map.get(key);
		} else {
			EntityViewInfo view = new EntityViewInfo();
			view.getSelector().add("days");
			view.getSelector().add("month");

			FilterInfo fi = new FilterInfo();
			fi.getFilterItems().add(
					new FilterItemInfo("workforceManagement.id",
							workforceManagementInfo.getId().toString()));
			fi.getFilterItems().add(new FilterItemInfo("group", group));
			fi.setMaskString("#0 and #1");
			view.setFilter(fi);

			collection = SchedulDetailFactory.getLocalInstance(ctx)
					.getSchedulDetailCollection(view);
			map.put(key, collection);
		}

		Calendar c = Calendar.getInstance();

		// if()
		c.setTime(beginDate);

		int year = c.get(Calendar.YEAR);

		String sql = "/*dialect*/DELETE FROM T_KQ_PERSONSCHEDULE WHERE FPERSONID=? AND FSCHEDULEYEAR=? AND TO_CHAR(FSCHEDULEDATE,'YYYY-MM-DD')>=?";
		DbUtil.execute(ctx, sql, new Object[] { personInfo.getId().toString(),
				year,DateUtils.date2String(beginDate, DateUtils.FORMAT_YMD)});

		CoreBaseCollection personScheduleCollection = new CoreBaseCollection();
		SchedulDetailInfo schedule = null;
		for (int i = 0; i < collection.size(); i++) {
			schedule = collection.get(i);
			String days = schedule.getDays();
			String month = schedule.getMonth();
			String[] dayArrs = days.split(",");

			for (int j = 0; j < dayArrs.length; j++) {
				String[] day = dayArrs[j].split(":", 2);
				c.set(Calendar.MONTH, Integer.parseInt(month) - 1);
				c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day[0]));

				String strDate1 = DateUtil.format(c.getTime(), "yyyy-MM-dd");
				String strDate2 = DateUtil.format(beginDate, "yyyy-MM-dd");

				if (strDate1.compareTo(strDate2) < 0) {
					continue;
				}

				if (day[1] != null && !day[1].equals("")) {
					PersonScheduleInfo personScheduleInfo = new PersonScheduleInfo();
					if (BOSUuid.read(day[1]).getType().toString().equals(
							"1F8115DE")) {
						CalendarTypeInfo rest = new CalendarTypeInfo();
						rest.setId(BOSUuid.read(day[1]));
						personScheduleInfo.setRest(rest);
					} else if (BOSUuid.read(day[1]).getType().toString()
							.equals("67912D14")) {
						WorkingTimeZoneMgrInfo workingTime = new WorkingTimeZoneMgrInfo();
						workingTime.setId(BOSUuid.read(day[1]));
						personScheduleInfo.setWorkTimeZone(workingTime);
					}
					personScheduleInfo.setScheduleYear(year);
					personScheduleInfo.setPerson(personInfo);
					personScheduleInfo.setScheduleDate(c.getTime());

					personScheduleCollection.add(personScheduleInfo);
				}
			}
		}
		PersonScheduleFactory.getLocalInstance(ctx).save(
				personScheduleCollection);
	}

	public void _processBeforeAddNew(Context arg0, IObjectValue arg1)
			throws BOSException, EASBizException {
	}

	public void _setBillState(Context ctx, BOSUuid arg1, HRBillStateEnum arg2)
			throws BOSException, EASBizException {

	}

	public void _update(Context arg0, IObjectPK arg1, IObjectValue arg2)
			throws BOSException, EASBizException {

	}
}
